<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>登录</title>
</head>

<body>
    <p></p>
    用户名<input type="text" /> 密码<input type="password" />
    <input type="button" value="登录" />
    <script src="ajax.js"></script>
    <script>
        const baseUrl = "http://localhost:8888";
        let oP = document.querySelector("p");
        let aInputs = document.querySelectorAll("input");
        aInputs[aInputs.length - 1].onclick = function () {
            //向接口传的数据
            let params = {
                username: aInputs[0].value,
                password: aInputs[1].value,
            };
            ajax({
                url: baseUrl + "/users/login",
                type: "post",
                params,
                fn(resData) {
                    console.log(resData);
                    resData = JSON.parse(resData);
                    if (resData.code == 0) {
                        oP.innerText = resData.message;
                    }
                    if (resData.code == 1) {
                        console.log(resData);
                        localStorage.setItem("nickname", resData.user.nickname);
                        localStorage.setItem("token", resData.token);
                        localStorage.setItem("uid", resData.user.id);
                        location.href = "03index.html";
                    }
                },
            });
        };
    </script>
</body>

</html>